package com.hjy.product.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.hjy.product.dao.AttrAttrgroupRelationDao;
import com.hjy.product.dao.AttrGroupDao;
import com.hjy.product.dao.CategoryDao;
import com.hjy.product.entity.AttrAttrgroupRelationEntity;
import com.hjy.product.entity.AttrGroupEntity;
import com.hjy.product.entity.CategoryEntity;
import com.hjy.product.service.AttrGroupService;
import com.hjy.product.service.CategoryService;
import com.hjy.product.vo.AttrRespVo;
import com.hjy.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hjy.common.utils.PageUtils;
import com.hjy.common.utils.Query;

import com.hjy.product.dao.AttrDao;
import com.hjy.product.entity.AttrEntity;
import com.hjy.product.service.AttrService;


@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {

    @Autowired
    private AttrGroupDao attrGroupDao;

    @Autowired
    private AttrAttrgroupRelationDao attrAttrgroupRelationDao;

    @Autowired
    private CategoryDao categoryDao;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrGroupService attrGroupService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                new QueryWrapper<AttrEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveInfo(AttrVo attr) {
        AttrEntity attrEntity = new AttrEntity();
        BeanUtil.copyProperties(attr, attrEntity);
        baseMapper.insert(attrEntity);

        // 1. 与属性分组建立关系
        // 销售数据不用做关联
        if (attrEntity.getAttrType() == 1) {
            AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
            attrAttrgroupRelationEntity.setAttrId(attrEntity.getAttrId());
            attrAttrgroupRelationEntity.setAttrSort(0);
            attrAttrgroupRelationEntity.setAttrGroupId(attr.getAttrGroupId());
            attrAttrgroupRelationDao.insert(attrAttrgroupRelationEntity);
        }

    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        return null;
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId, String type) {
        QueryWrapper<AttrEntity> wrapper = new QueryWrapper<>();
        if ("base".equals(type)) {
            wrapper.eq("attr_type", 1);
        } else {
            wrapper.eq("attr_type", 0);
        }
        if (catelogId != 0) {
            wrapper.eq("catelog_id", catelogId);
        }
        String key = (String) params.get("key");
        if (StrUtil.isNotBlank(key)) {
            wrapper.and(attrEntityQueryWrapper -> {
                attrEntityQueryWrapper.eq("attr_id", key).or().like("attr_name", key);
            });
        }

        IPage<AttrEntity> page = this.page(new Query<AttrEntity>().getPage(params), wrapper);
        PageUtils pageUtils = new PageUtils(page);
        List<AttrEntity> records = page.getRecords();
        List<AttrRespVo> collect = records.stream().map(attrEntity -> {
            AttrRespVo attrRespVo = new AttrRespVo();
            BeanUtil.copyProperties(attrEntity, attrRespVo);

            // 1. 构造 attr_group_name
            if ("base".equals(type)) {
                AttrAttrgroupRelationEntity attr_id = attrAttrgroupRelationDao.selectOne(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrEntity.getAttrId()));
                if (attr_id != null) {
                    Long attrGroupId = attr_id.getAttrGroupId();
                    AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(attrGroupId);
                    attrRespVo.setGroupName(attrGroupEntity.getAttrGroupName());
                    // 2. 构造 category_name
                }
            }

            CategoryEntity cat_id = categoryDao.selectOne(new QueryWrapper<CategoryEntity>().eq("cat_id", attrEntity.getCatelogId()));
            if (cat_id != null) {
                attrRespVo.setCatelogName(cat_id.getName());
            }

            return attrRespVo;
        }).collect(Collectors.toList());

        pageUtils.setList(collect);
        return pageUtils;
    }

    @Override
    public AttrRespVo getAttrInfo(Long attrId) {
        AttrEntity attrEntity = this.getById(attrId);
        AttrRespVo attrRespVo = new AttrRespVo();
        BeanUtil.copyProperties(attrEntity, attrRespVo);

        // 1. 先查出分组的 id 和名字
        AttrAttrgroupRelationEntity attr_id = attrAttrgroupRelationDao.selectOne(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrId));
        if (attr_id != null) {
            attrRespVo.setAttrGroupId(attr_id.getAttrGroupId());
            AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(attr_id.getAttrGroupId());
            if (attrGroupEntity != null) {
                attrRespVo.setGroupName(attrGroupEntity.getAttrGroupName());
            }
        }

        // 2. 查出三级分类
        List<Long> threeFloorCatId = attrGroupService.getThreeFloorCatId(attrEntity.getCatelogId());
        if (threeFloorCatId != null) {
            attrRespVo.setCatelogPath(threeFloorCatId);
        }
        return attrRespVo;
    }

    @Override
    public List<AttrEntity> getListByGroupId(Long attrGroupId) {
        List<AttrAttrgroupRelationEntity> attrGroups = attrAttrgroupRelationDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", attrGroupId));
        List<Long> collect = attrGroups.stream().map(attrAttrgroupRelationEntity -> {
            return attrAttrgroupRelationEntity.getAttrId();
        }).collect(Collectors.toList());

        List<AttrEntity> list = baseMapper.selectList(new QueryWrapper<AttrEntity>().in("attr_id", collect));


        return list;
    }

}